package su.product.module;

/**
 * 分类管理
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import su.product.entity.Cate;
import su.product.service.CateService;
import su.tool.SuIntUtils;
@Controller
@RequestMapping("/admin/cate")
public class CateServlet {

	@Autowired
	private CateService cateService;

	protected final Log log = LogFactory.getLog(getClass());
	/**
	 * 显示分类树
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("show")
	public String show(HttpServletRequest request, Model model){
		model.addAttribute("productType", request.getParameter("productType"));
		return "/maintain/product/cate_show";
	}
	/**
	 * 编辑分类树
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("edit")
	public String edit(HttpServletRequest request, Model model){
		return "/maintain/product/cate_edit";
	}
	/**
	 * 子分类信息
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("getSubCate")
	public void getSubCate(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer == null)return;
		String res = "";
		String treeType = request.getParameter("treeType");//分类树类型，1查看，2编辑
		String productType = request.getParameter("productType");//(查看树时用到)产品类型，1产品2原料
		String pid = request.getParameter("id");//父级分类id
		if(StringUtils.isBlank(pid)){
			if(StringUtils.isNotBlank(productType))pid=productType;
			else pid="0";
		}
		List<Cate> list = cateService.getAllCates(Integer.parseInt(pid));
		if(list != null && !list.isEmpty()){
			for(Cate one : list){
				if(StringUtils.isNotBlank(res))res += ",";
				String cateName = one.getCatename();
				if("1".equals(treeType))cateName += "_"+one.getId();//查看树时title显示分类的id
				res += "{id:'"+one.getId()+"',name:'"+cateName+"',isParent:"+(one.getLeaf()==2);
				if("1".equals(productType)){//产品
					res += ",url:'"+request.getContextPath()+"/admin/product/getProductsByCate.action?cateId="+one.getId()+"',target:'product_frame_main'";
				}
				if("2".equals(productType)){//原料
					res += ",url:'"+request.getContextPath()+"/admin/material/getMaterialsByCate.action?cateId="+one.getId()+"',target:'product_frame_main'";
				}
				
				res += "}";
			}
			res = "["+res+"]";
		}
		
		writer.print(res);
		writer.flush();
		writer.close();
	}

	/**
	 * 添加一个分类
	 * @param request
	 * @param response
	 * @param cate
	 * @param model
	 */
	@RequestMapping("add_cate")
	public void addCate(HttpServletRequest request,HttpServletResponse response,Cate cate, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer == null)return;
		cate.setLeaf(1);
		cate.setStatus(1);
		String res = "";
		try {
			res = cateService.addCate(cate);
		} catch (Exception e) {
		}
		
		writer.print(res);
		writer.flush();
		writer.close();
	}
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("do_remove")
	public void doRemove(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer==null)return;
		String res = "";
		int id = SuIntUtils.getInt(request.getParameter("id"));
		
		if(id==0){
			res = "请选择要删除的数据";
		}else{
			try {
				res = cateService.remove(id);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(StringUtils.isBlank(res)){
				res = "success";	
			}
			
		}
		
		writer.print(res);
		writer.flush();
		writer.close();
    }
	
	@RequestMapping("do_update_catename")
	public void doUpdateCateName(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer==null)return;
		String res = "";
		int id = SuIntUtils.getInt(request.getParameter("id"));
		String name = request.getParameter("catename");
		
		if(id==0){
			res = "请选择要修改的数据";
		}else{
			if(StringUtils.isBlank(name)){
				res = "分类名称不能为空";
			}else{
				try {
					cateService.updateCateName(id, name);
				} catch (Exception e) {
					log.error(e.getMessage());
				}
				
				res = "success";
			}
			
		}
		
		writer.print(res);
		writer.flush();
		writer.close();
    }
}
